﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMSClassLibrary;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSendSMS_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 1;
                FileStream fs = null;
                string sFileName = "";
                if (chkTest.Checked)
                {
                    sFileName = @"SafeCopyOfNos.txt";
                }
                else
                {
                    sFileName = @"Numbers.txt";
                }
                if (File.Exists(sFileName))
                {
                    fs = new FileStream(sFileName, FileMode.Open);
                }

                StreamReader sr = new StreamReader(fs);
                string message = rtbMessage.Text;

                while (true)
                {
                    string sNo = sr.ReadLine();
                    if (String.IsNullOrEmpty(sNo))
                    {
                        WriteToErrorFile("Done");
                        break;
                    }
                    try
                    {
                        string sRes = SendSMS(sNo, message);
                        WriteToErrorFile("Total:"+count +" ***Sent :" + sNo + " *** Result :"+sRes);

                        count++;
                    }
                    catch (System.Exception ex)
                    {
                        WriteToErrorFile("Error with No " + sNo + " :" + ex.Message);
                    }
                }
                sr.Close();
                fs.Close();
                MessageBox.Show("Done");
            }
            catch (System.Exception ex)
            {
                //Wr
            }

        }
        private void ShowResult(int iCount)
        {
            lblResult.Text = "Done :"+iCount.ToString();
        }
        private string SendSMS(string number, string message)
        {
            ReddyInfoSoft ris = new ReddyInfoSoft();
            string res = ris.sendSMS("9311207547", "daalchini", message, number, "way2sms");
            return res;
        }
        public static void WriteToErrorFile(string str)
        {
            FileStream fs = null;
            //string sFileName = GetFileName(sType);
            if (File.Exists("log.txt"))
            {
                fs = new FileStream("log.txt", FileMode.Append);
            }
            else
            {
                fs = new FileStream("log.txt", FileMode.Create);
            }
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Environment.NewLine);
            sw.WriteLine(str);
            sw.Close();
            fs.Close();

        }
        delegate void ShowUI(int digits);
        private void btnTestThread_Click(object sender, EventArgs e)
        {

            ShowUI calcPi = new ShowUI(ShowResult);
            for (int i = 0; i < 1000; i++)
            {
                calcPi.BeginInvoke(i, null, null);
            }
        }

    }
}
